package org.ahuanlien.crm.web.controller;

import org.ahuanlien.crm.domain.Department;
import org.ahuanlien.crm.query.QueryObject;
import org.ahuanlien.crm.service.IDepartmentService;
import org.ahuanlien.crm.util.JSONResult;
import org.ahuanlien.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("department")
public class DepartmentController {

	@Autowired
	private IDepartmentService departmentService;

	@RequestMapping("selectAll")
	@ResponseBody
	public List<Department> selectAll(QueryObject qo) {
		return departmentService.selectAll();
	}

	//打开页面和数据获取分开
	@RequestMapping("view")
	public String view() {
		return "department/view";
	}

	@RequestMapping("query")
	@ResponseBody
	public PageResult query(QueryObject qo) {
		PageResult result = departmentService.query(qo);
		return result;
	}
	@RequestMapping("saveOrUpdate")
	@ResponseBody
	public JSONResult saveOrUpdate(Department department) {
		try {
			departmentService.saveOrUpdate(department);
		} catch (Exception e) {
			e.printStackTrace();
			return JSONResult.mark("操作失败");
		}
		return JSONResult.success("操作成功");
	}
	@RequestMapping("delete")
	@ResponseBody
	public JSONResult delete(Long id) {
		try {
			departmentService.deleteByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			return JSONResult.mark("操作失败");
		}
		return JSONResult.success("操作成功");
	}
}
